// components/index/FilterPop/FilterPop.js
import http from '../../../utils/http'
var cityData = require('../../../utils/city.js');

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    curTab: {
      type: Number,
      value: 0
    },
    cityCode: {
      type: String,
      value: ''
    },
    cityName: {
      type: String,
      value: ''
    }
  },
  
  /**
   * 组件的初始数据
   */
  data: {
    isHideFilter: true,
    curTab: 0,
    navMap: [
      {
        text: '区域',
        value: 1
      },
      {
        text: '类型',
        value: 2
      },
      {
        text: '默认排序',
        value: 3
      }
      // ,
      // {
      //   text: '筛选',
      //   value: 4
      // }
    ],
    isScroll: true,
    // isHideFilter: true,
    //brandList:[],
    selStarIds:{},
    selBrandIds:{},
    content: [],
    isfirst: true,
    isfull: false,
    isOnlyRegin: true,
    regions: [],
    blocks: [],
    select1: '',
    select2: '',
    select3: '',
    currType: '',
    currRank: '',
    //currentText: '',
    currentReginText: '区域',
    currentTypeText: '类型',
    currentRankText: '默认排序',
    currentFilterText: '筛选',
    cityCode: '310100',
    cityName: ''
  },
  attached:function(){
    //this.getBrandList()
    this.setData({
      cityCode: wx.getStorageSync('cityCode') || '310100',
      cityName: wx.getStorageSync('cityName') || '上海',
      rankList: [
        { key: 0, value: '默认排序' },
        { key: 1, value: '面积从大至小' },
        { key: 2, value: '面积从小至大' }
      ],
      typeList: [
        { typeValue: "", typeName: "全部" },
        { typeValue: "1", typeName: "办公用房" },
        { typeValue: "2", typeName: "商业用房" },
        { typeValue: "3", typeName: "住宅用房" },
        { typeValue: "4", typeName: "工业用房" },
        { typeValue: "5", typeName: "其他" }
      ]
    });
    console.log('=====',this.data.cityCode);
    this.getBlock(this.data.cityCode);
    //this.getTypes();
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeTab: function (e) {
      wx.stopPullDownRefresh();
      let that = this;
      that.data.isfull ? wx.showTabBar({ animation: true }) : wx.hideTabBar({ animation: true });
      var idx = e.currentTarget.dataset.idx;
      that.setData({
        curTab: idx,
        isfull: !that.data.isfull,
        isScroll: false
      });
      if (idx === '1' && that.data.isfull){
        console.log('====idx==1');
        // this.setData({
        //   cityCode: wx.getStorageSync('cityCode')
        // });
        that.getBlock(that.data.cityCode);
      }
    },
    goCity: function () {
      wx.showTabBar({ animation: true });
      wx.navigateTo({
        url: '/pages/index/cityList/cityList?code=' + this.data.cityCode + '&name=' + this.data.cityName
      });
      this.resetFilters();
    },
    getBlock: function (pam) {
      console.log('pam:', pam, 'that.data.cityCode:', this.data.cityCode);
      var that = this;
      http({
        url: '/' + that.data.cityCode +'/region/list',
        method: 'POST',
        data: {
          // cityCode: that.data.cityCode
        },
        success: function (res) {
          //console.log('====getBlock res:', res);
          if (!res) return;
          that.setData({
            regions: res.data,
            isScroll: false
          })
        }
      })
    },
    getTypes: function(){
      var that = this;
      http({
        url: '/' + that.data.cityCode + '/house/types',
        method: 'POST',
        data: {
          cityCode: that.data.cityCode
        },
        success: function (res) {
          //console.log('====getTypes res:', res);
          if (!res) return;
          that.setData({
            ranks: res.data
          })
        }
      })
    },

    showBarFilter: function () {
      this.triggerEvent('showfilter');
      this.setData({
        isfull: false
      });
    },

    resetFilters: function(){
      //console.log('###resetFilters');
      this.triggerEvent('resetfilter');
      this.setData({
        blocks: [],
        select1: '',
        select3: '',
        currType: '',
        currRank: '',
        currentReginText: '区域',
        currentTypeText: '类型',
        currentRankText: '默认排序',
        currentFilterText: '筛选',
        isfull: false,
        isOnlyRegin: true
      });
    },

    selectRegin: function (e) {
      console.log('cityleft e:', e.currentTarget.dataset);
      let datas = e.currentTarget.dataset;
      this.setData({
        isOnlyRegin: false,
        blocks: datas.blocks,
        select1: datas.idx,
        select3: ''
      });
    },
    refreshData: function(e){
      wx.showTabBar({ animation: true });
      let that = this, 
          param,
          target = e.currentTarget.dataset,
          paramKey = target.type,
          filter = target.filter;
      //console.log('====refreshData id:', paramKey);
      if(filter==='TYPE'){
        that.setData({
          currType: paramKey,
          currentTypeText: target.value,
          isfull: false
        });
        //传递给父组件中
        this.triggerEvent('myevent', { curFilter:'TYPE', curParam: that.data.currType });
      } else if (filter === 'RANK'){
        that.setData({
          currRank: paramKey,
          currentRankText: target.value,
          isfull: false
        });
        this.triggerEvent('myevent', { curFilter: 'RANK', curParam: that.data.currRank });
      } else{
        that.setData({
          select3: target.idx,//paramKey,
          currentReginText: target.value === '全部' ? that.data.regions[that.data.select1].name : target.value,
          isfull: false
        });
        console.log('====select3:', that.data.select3, 'select1:', that.data.select1, that.data.regions[that.data.select1]);
        this.triggerEvent('myevent', { curFilter: 'BLOCK', curParam: target.code });
      }
    },
    hidebg: function (e) {
      wx.showTabBar({ animation: true });
      this.setData({
        isHideFilter: true,
        isScroll: true,
        isfull: false,
        curTab: 0
      })
    }
  }
})